package com.zzsy.carosshopprojects.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.TypedValue;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 自定义RecyleView item的间距
 */
public class GridDividerItemDecoration extends RecyclerView.ItemDecoration {
    private Context context;
    private int span;

    public GridDividerItemDecoration(Context context, int span) {
        this.context = context;
        this.span = span;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        GridLayoutManager.LayoutParams lp = (GridLayoutManager.LayoutParams) view.getLayoutParams();
        int span = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
                this.span, context.getResources().getDisplayMetrics());

        if (lp.getSpanIndex() == 0) {
            outRect.left = 0;
        } else {
            outRect.left = span;
        }



//        if (position < 2) {
//            outRect.top = 0;
//        } else {
//            outRect.top = span;
//        }
        outRect.top = span;
    }
}
